package runnerworm.ui;

import java.util.*;
import javax.swing.*;
import java.awt.*;
import runnerworm.core.*;

/**
 * <p>Title: UserInterfaceEventLauncher</p>
 * <p>Description: This class has only one meaning: launch UserInterfaceEvents.</p>
 * @author Guilherme Mauro Germoglio Barbosa
 */

public class UserInterfaceEventLauncher extends Component{
  /**
   * This is the event queue.
   */
  private static EventQueue eventQueue;
  /**
   * This will be the UserInterfaceEvents listener
   */
  private UserInterfaceListener listener;

  /**
   * The constructor. It will enable the events launching.
   */
  public UserInterfaceEventLauncher() {
    this.eventQueue = Toolkit.getDefaultToolkit().getSystemEventQueue();
    enableEvents(0);
  }

  /**
   * Adds a listener to this class' events.
   * @param l The UserInterfaceEvents listener.
   */
  public void addUserInterfaceListener(UserInterfaceListener l) {
    listener = l;
  }

  /**
   * Maybe puts the event on the event queue, or something like that.
   * @param evt The event will be put on the queue
   */
  public void processEvent(AWTEvent evt) {
    if (evt instanceof UserInterfaceEvent) {
      if (listener != null)
        switch(evt.getID()) {
          case UserInterfaceEvent.ABOUT:
            listener.aboutSelected((AboutEvent) evt);
            break;
          case UserInterfaceEvent.HELP:
            listener.helpSelected((HelpEvent) evt);
            break;
          case UserInterfaceEvent.HIGH_SCORES:
            listener.highScoresSelected((HighScoresEvent) evt);
            break;
          case UserInterfaceEvent.NEW_GAME:
            NewGameEvent newGameEvent = (NewGameEvent)evt;
            listener.newGameSelected(newGameEvent.getGameOptions(),
                                     newGameEvent.getScore(),
                                     newGameEvent.getJPanel());
            break;
          case UserInterfaceEvent.OPTIONS:
            listener.optionsSelected((OptionsEvent) evt);
            break;
          case UserInterfaceEvent.QUIT_GAME:
            listener.quitSelected((QuitGameEvent) evt);
            break;
          case UserInterfaceEvent.WORM_GOING_DOWN:
            listener.wormGoesDown();
            break;
          case UserInterfaceEvent.WORM_GOING_UP:
            listener.wormGoesUp();
            break;
          case UserInterfaceEvent.PLAYER_NAME:
            listener.setPlayerName(((PlayerNameEvent)evt).getPlayerName());
            break;
        }
    }
    else
      super.processEvent(evt);
  }

  /**
   * Creates and launches a NewGameEvent.
   * @param gameOptions The game's options
   * @param score The game's score
   * @param panel Where the game will be shown.
   */
  public void createNewGameEvent(GameOptions gameOptions, JLabel score, JPanel panel) {
    eventQueue.postEvent(new NewGameEvent(this, gameOptions, score, panel));
  }

  /**
   * Creates and launches a OptionsEvent.
   */
  public void createOptionsEvent() {
    eventQueue.postEvent(new OptionsEvent(this));
  }

  /**
   * Creates and launches a HighScoresEvent.
   */
  public void createHighScoresEvent() {
    eventQueue.postEvent(new HighScoresEvent(this));
  }

  /**
   * Creates and launches a QuitGameEvent.
   */
  public void createQuitGame() {
    eventQueue.postEvent(new QuitGameEvent(this));
  }

  /**
   * Creates and launches a HelpEvent.
   */
  public void createHelpEvent() {
    eventQueue.postEvent(new HelpEvent(this));
  }

  /**
   * Creates and launches a AboutEvent.
   */
  public void createAboutEvent() {
    eventQueue.postEvent(new AboutEvent(this));
  }

  /**
   * Creates and launches a WormGoingUpEvent.
   */
  public void createWormGoingUpEvent() {
    eventQueue.postEvent(new WormGoingUpEvent(this));
  }

  /**
   * Creates and launches a WormGoingDownEvent.
   */
  public void createWormGoingDownEvent() {
    eventQueue.postEvent(new WormGoingDownEvent(this));
  }

  /**
   * Creates and launchers a PlayerNameEvent.
   * @param name The player's name.
   */
  public void createPlayerNameEvent(String name) {
    eventQueue.postEvent(new PlayerNameEvent(this, name));
  }

}









